﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BlogDH.User
{
    public partial class BlogInfo : BasePage
    {
        protected string errname = string.Empty;
        protected string errtags = string.Empty;
        protected string errdesc = string.Empty;
        protected Model.BlogInfo blogInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetBloginfo();
            }
            if (Request.HttpMethod == "POST")
            {
                SaveBlogInfo();
            }
        }

        private void GetBloginfo()
        {
            if (blogInfo == null)
                blogInfo = new BLL.BlogInfo().GetModel(UserID, UserName);
            if (blogInfo == null)
            {
                Response.Write("<script>window.location.href='/login.aspx'</script>");
                Response.End();
            }


        }

        private void SaveBlogInfo()
        {
            string bname = Request.Form["bname"];
            string btag = Request.Form["btag"];
            string bdesc = Request.Form["bdesc"];
            int iswww = 0;
            int.TryParse(Request.Form["iswww"], out iswww);
            int isshow = 0;
            int.TryParse(Request.Form["isshow"], out isshow);
            int year = 0;
            int.TryParse(Request.Form["year"], out year);
            int month = 0;
            int.TryParse(Request.Form["month"], out month);

            if (bname.Length > 9)
            {
                errname = "超过最大长度";
            }
            else if (btag.Length > 16)
            {
                errtags = "超过最大长度";
            }
            else if (bdesc.Length > 96)
            {
                errdesc = "超过最大长度";
            }
            else
            {
                iswww = iswww == 1 ? 1 : 0;
                isshow = isshow == 1 ? 1 : 0;

                if (year < 2001 || year > System.DateTime.Now.Year)
                    year = System.DateTime.Now.Year;

                if (month < 0 || month > 12)
                    month = System.DateTime.Now.Month;

                Model.BlogInfo model = new Model.BlogInfo()
                {
                    BlogName = bname,
                    BlogDesc = bdesc,
                    BlogTags = btag,
                    CreateYear = year,
                    CreateMouth = month,
                    IsWWW = iswww,
                    IsShow = isshow,
                    Userid = base.UserID,
                    BlogUrl = base.UserName
                };

                bool flag = new BLL.BlogInfo().Update(model);

                if (flag)
                {
                    blogInfo.BlogName = bname;
                    blogInfo.BlogDesc = bdesc;
                    blogInfo.BlogTags = btag;
                    blogInfo.CreateYear = year;
                    blogInfo.CreateMouth = month;
                    blogInfo.IsWWW = iswww;
                    blogInfo.IsShow = isshow;
                    blogInfo.Userid = base.UserID;
                    blogInfo.BlogUrl = base.UserName;
                    Response.Write("<script>alert('博客信息修改成功')</script>");
                }
                else
                {
                    Response.Write("<script>alert('博客信息修改失败')</script>");
                }
            }

        }
    }
}